# -*- coding: UTF-8 -*-

# FileName     : response
# Author       : EastsunW eastsunw@foxmail.com
# Create at    : 2022-04-22 18:19
# Last Modified: 2022-04-22 18:19
# Modified By  : EastsunW
# -------------
# Description  : 用于TF、gene、SNP的自动补全，输入部分名字，返回匹配结果
# -------------

from flask_restful import Resource, fields, marshal_with, reqparse
from Server.Utils.MongoDB import mongo


class Hint_Gene_Class(Resource):
    # 返回数据的格式
    response_fields = {
        "genes": fields.List(fields.String)
    }

    @marshal_with(response_fields)
    def post(self):
        """返回TF可选列表
        Arguments：
            query (str) : 输入的内容
        Returns:
            {
                genes : String[]
            }
        """
        # 获取参数：当前页码，每页结果数，查询类型和查询名称
        parser = reqparse.RequestParser()
        parser.add_argument(
            'query', type=str,
            required=True,
            nullable=False
        )
        request_args = parser.parse_args()
        raw_result = mongo.db\
            .get_collection("fimo")\
            .distinct(
                key="gene_symbol",
                filter={
                    "gene_symbol": {
                        "$regex": "^"+request_args["query"]
                    }
                }
            )
        response_result = raw_result[0:30]
        return {"genes": response_result}
